﻿using GDI_应用案例.Pages.Controls;
using GDI_应用案例.Pages.Controls._3D;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GDI_应用案例.Pages
{
    [FormPage(6, "3D效果")]
    public partial class Page3D : PageBase
    {
        public Page3D()
        {
            InitializeComponent();
            this.DoubleBuffered = true;

            _rect3D = new Rect3D(new Point3D(this.Width - 100 >> 1, 150,100), new RectangleF3D(100, 100, 150, 200, 100, 300));
            _rect3D.Distance = 1200;
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
            _rect3D.OnMouseDown(e);
           
        }
        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);
            _rect3D.OnMouseUp(e);
        }
        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);
            _rect3D.OnMouseLeave(e);
        }
        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
            _rect3D.OnMouseMove(e);
            this.Invalidate();
        }
        protected override void OnMouseWheel(MouseEventArgs e)
        {
            base.OnMouseWheel(e);
            _rect3D.OnMouseWheel(e);
            this.Invalidate();
        }

        readonly Rect3D _rect3D;
        Pen _pen = new Pen(Color.Red, 2);
        string _text = "1.按住 Shift 键+鼠标拖动->旋转；\r\n2.鼠标按住拖动->移动；\r\n3.鼠标滚轮滚动->放大缩小。";
        Brush _brush= new SolidBrush(Color.Black);
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Graphics g = e.Graphics;
            g.Smooth();
            g.DrawString(_text, this.Font, _brush, 10, 10);

           Stopwatch sw = Stopwatch.StartNew();
            _rect3D.Draw(g, _pen);
            sw.Stop();
            Trace.WriteLine($"耗时：{sw.ElapsedMilliseconds}ms");
        }

        private void DrawTriangularPrism3D(Graphics g)
        {
            // 定义三棱柱的顶点
            Point3D[] prism = new Point3D[]
     {
        // 底面三角形
        new Point3D(150, 250, 100),  // 0
        new Point3D(250, 250, 100), // 1
        new Point3D(200, 150, 100),  // 2
        // 顶面三角形
        new Point3D(150, 250, 200), // 3
        new Point3D(250, 250, 200), // 4
        new Point3D(200, 150, 200)   // 5
     };

            g.DrawTriangularPrism3D(prism);
        }

        private void Page3D_FormClosing(object sender, FormClosingEventArgs e)
        {
            _rect3D.Dispose();
        }
    }

    public static class Point3DEx
    {

        private static float _distance = 500;

        static readonly  // 定义三棱柱的边
            int[,] edgesprism = new int[,]
    {
        // 底面三角形
        {0, 1}, {1, 2}, {2, 0},
        // 从底面到顶面
        {0, 3}, {3, 4}, {4, 1}, // 从底面到顶面
        // 顶面三角形
        {4, 5}, {5, 3},
        // 从顶面回到底面
        {5, 2}
    };
        public static void DrawTriangularPrism3D(this Graphics g, Point3D[] shape)
        {
            PointF[] pointFs = new PointF[shape.Length];
            for (int i = 0; i < shape.Length; i++)
            {
                pointFs[i] = shape[i].ToPointF(_distance);
            }
            Draw(g, pointFs, edgesprism);
        }
        public static void Draw(this Graphics g, PointF[] pf, int[,] edges)
        {
            Pen pen = new Pen(Color.Black, 2);
            int len = edges.GetLength(0);
            PointF[] points = new PointF[len << 1];
            int index = 0;
            for (int i = 0; i < len; i++)
            {
                PointF point1 = pf[edges[i, 0]];
                PointF point2 = pf[edges[i, 1]];
                points[index++] = point1;
                points[index++] = point2;
            }
            g.DrawLines(pen, points);
            pen.Dispose();
        }

    }
}
